import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {ModalDirective} from "ngx-bootstrap";
import {AliyunOssOutputUrl, tuikeSystemUrl} from "../../../config/api";
import {HttpService} from "../../lib/service/http.service";
import {Headers, RequestOptions} from "@angular/http";
import * as moment from 'moment';
import {Router} from "@angular/router";

@Component({
  selector: 'app-examine',
  templateUrl: './examine.component.html',
  styleUrls: ['./examine.component.scss']
})
export class ExamineComponent implements OnInit {
  aliyunOssOutputUrl = AliyunOssOutputUrl;
  searchData = '';
  status = '3';

  type: string;
  message: string;
  isShow = true;
  totalItems: number;
  currentPage: number = 1;
  itemsPerPage: number = 20;
  _isSpinning = false;

  editItem;
  apply_status = '';
  editSrc = '';
  examineList = [];
  remark = '';
  searchType = 'member_mobile';
  invited = null;
  selectedOption;
  examineTotal;
  add_apply_status = '1';
  _startDate = null;
  _endDate = null;
  startDate = null;
  endDate = null;
   sex = null;

  @ViewChild('passModal') public passModal: ModalDirective;


  _console(value) {
    console.log(value);
  }
  keyDown(e) {
    e.code === 'Enter' ? this.search() : '';
  }

  _startValueChange = () => {
    if (this._startDate > this._endDate) {
      this._endDate = null;
    }
  }
  _endValueChange = () => {
    if (this._startDate > this._endDate) {
      this._startDate = null;
    }
  }
  _disabledStartDate = (startValue) => {
    if (!startValue || !this._endDate) {
      return false;
    }
    return startValue.getTime() >= this._endDate.getTime();
  }
  _disabledEndDate = (endValue) => {
    if (!endValue || !this._startDate) {
      return false;
    }
    return endValue.getTime() <= this._startDate.getTime();
  }

  constructor(private httpService: HttpService, private el: ElementRef, private router: Router) {
  }

  ngOnInit() {
    this.getExamineList(1);
  }

  //重置
  reset() {
    this.searchData = '';
    this.searchType = 'member_mobile';
    this.invited = false;
    this._startDate = null;
    this._endDate = null;
    this.currentPage = 1;
    this.sex = null;
    this.getExamineList(1);
  }

  //搜索
  search() {
    this.getExamineList(1);
  }

  showPreview(e) {
    let task = this.el.nativeElement.querySelector('.mask');
    task.style.display = "block";
    this.editSrc = e.srcElement.currentSrc;
  }

  console(e) {
    console.log(this.searchType);
  }

  closeMask() {
    this.el.nativeElement.querySelector('.mask').style.display = 'none';
  }

  showPassModal(item) {
    this.editItem = item;
    this.passModal.show();
  }


  clearSearch() {
    this.searchData = '';
    this.searchType = 'member_mobile';
    this.invited = false;
    this._startDate = null;
    this._endDate = null;
    this.currentPage = 1;
    this.getExamineList(1);
  }
  goDetail(id) {
    const url = `${tuikeSystemUrl}/api/v1/admin/member_apply/apply_info?id=${id}`;
    this.httpService.httpGetAuth(url).subscribe(res => {
    });
  }

  getExamineList(page) {
    this._isSpinning = true;
    this.startDate = this._startDate ? moment(this._startDate).format('YYYY-MM-DD 00:00:00') : '';
    this.endDate = this._endDate ? moment(this._endDate).format('YYYY-MM-DD 23:59:59') : '';
    let url = '';
    if(this.sex) {
      url = `${tuikeSystemUrl}/api/v1/admin/member_apply/apply_list?page=${page}&pageSize=${this.itemsPerPage}&${this.searchType}=${this.searchData}&invited=${this.invited? 1 : 0}&sex=${this.sex}&apply_status=${this.apply_status}&start_date=${this.startDate}&end_date=${this.endDate}`;
    } else {
      url = `${tuikeSystemUrl}/api/v1/admin/member_apply/apply_list?page=${page}&pageSize=${this.itemsPerPage}&${this.searchType}=${this.searchData}&invited=${this.invited? 1 : 0}&apply_status=${this.apply_status}&start_date=${this.startDate}&end_date=${this.endDate}`;
    }
    this.httpService.httpGetAuth(url).subscribe(res => {
      this.examineList = res.data.data;
      this.examineTotal = res.data.total;
      this._isSpinning = false;
    })
  }

  addPassFormSubmit() {
    const url = `${tuikeSystemUrl}/api/v1/admin/member_apply/apply_check`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({id: this.editItem.id, apply_status: parseInt(this.add_apply_status), remark: this.remark});
    this.httpService.httpPostAuth(url, body, options).subscribe(res => {
      if(res.code === 200) {
        this.type = 'success';
        this.message = '提交成功';
        this.isShow = true;
        this.remark = '';
        this.currentPage = 1;
        this.add_apply_status = '1';
        this.passModal.hide();
        this.getExamineList(1);
      }
    })
  }

  pageChanged(event: any): void {
    window.scrollTo(0, 0);
    this.getExamineList(event.page);
  }

}
